package com.demarc.pesquisas.infraestrutura.web.jsf.component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.component.messages.Messages;
import org.primefaces.component.messages.MessagesRenderer;

public class CustomMessagesRenderer extends MessagesRenderer {

	@Override
	public void encodeEnd(FacesContext context, UIComponent component)
			throws IOException {
		Messages uiMessages = (Messages) component;
		ResponseWriter writer = context.getResponseWriter();
		String id = uiMessages.getId();
		String clientId = uiMessages.getClientId(context);
		Map<String, List<FacesMessage>> messagesMap = new HashMap<String, List<FacesMessage>>();

		String theFor = uiMessages.getFor();
		List<FacesMessage> messages = new ArrayList<FacesMessage>();
		if (theFor != null) {
			messages.addAll(context.getMessageList(theFor));
		} else {
			messages.addAll(context.getMessageList(null));
			if (!uiMessages.isGlobalOnly()) {
				String showSpecificsAttribute = String.valueOf(uiMessages
						.getAttributes().get("showSpecifics"));
				boolean showSpecifics = Boolean
						.parseBoolean(showSpecificsAttribute);

				Iterator<String> clientIDs = context.getClientIdsWithMessages();
				while (clientIDs.hasNext()) {
					String clientID = clientIDs.next();
					if (StringUtils.isNotBlank(clientID)) {

						if (showSpecifics) {
							UIComponent uiComponent = JsfComponentUtils
									.findComponentByExactClientId(clientID);
							if (uiComponent != null) {
								String specificsAttribute = String
										.valueOf(uiComponent.getAttributes()
												.get("specifics"));
								boolean specifics = id
										.equals(specificsAttribute);
								if (specifics) {
									messages.addAll(context
											.getMessageList(clientID));
								}
							}
						} else {
							UIComponent uiComponent = JsfComponentUtils
									.findComponentByExactClientId(clientID);
							if (uiComponent != null) {
								Object specificsAttribute = uiComponent
										.getAttributes().get("specifics");
								if (specificsAttribute == null) {
									messages.addAll(context
											.getMessageList(clientID));
								}
							}
						}
					}
				}
			}
		}

		for (FacesMessage message : messages) {
			FacesMessage.Severity severity = message.getSeverity();

			if (severity.equals(FacesMessage.SEVERITY_INFO)) {
				addMessage(uiMessages, message, messagesMap, "info");
			} else if (severity.equals(FacesMessage.SEVERITY_WARN)) {
				addMessage(uiMessages, message, messagesMap, "warn");
			} else if (severity.equals(FacesMessage.SEVERITY_ERROR)) {
				addMessage(uiMessages, message, messagesMap, "error");
			} else if (severity.equals(FacesMessage.SEVERITY_FATAL)) {
				addMessage(uiMessages, message, messagesMap, "fatal");

			}
		}

		writer.startElement("div", uiMessages);
		writer.writeAttribute("id", clientId, "id");
		writer.writeAttribute("class", "ui-messages ui-widget", null);
		writer.writeAttribute("aria-live", "polite", null);

		for (String severity : messagesMap.keySet()) {
			List<FacesMessage> severityMessages = messagesMap.get(severity);

			if (severityMessages.size() > 0) {
				encodeSeverityMessages(context, uiMessages, severity,
						severityMessages);
			}
		}

		writer.endElement("div");
	}

}
